package com.ygj.inspection.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.json.JsonUtil;
import com.base.common.persistent.model.Employee;
import com.cjy.common.bean.OpAttributes;
import com.gexin.rp.sdk.base.uitls.MD5Util;
import com.ygj.inspection.service.InspectionService;

/**
 * 巡查Action
 * 
 * @author Kevin Colin
 * @datetime 2015年9月5日下午4:02:14
 * 
 */
@Action("inspectionAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class InspectionAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private InspectionService inspectionService;

	private String inspectionPointCode;
	private int inspectionCategories;
	private String inspectionProjectsStatus;
	private int compoundsId; // 当前小区id
	private String coordinates; // 经纬度
	private String code; // 巡查点编码
	private String startTime;
	private String endTime;
	private int hour;
	private int id;
	private int flag;	// 巡查方式标识，0，扫码，1，摇一摇

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getInspectionPointCode() {
		return inspectionPointCode;
	}

	public void setInspectionPointCode(String inspectionPointCode) {
		this.inspectionPointCode = inspectionPointCode;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getInspectionCategories() {
		return inspectionCategories;
	}

	public void setInspectionCategories(int inspectionCategories) {
		this.inspectionCategories = inspectionCategories;
	}

	public String getCoordinates() {
		return coordinates;
	}

	public void setCoordinates(String coordinates) {
		this.coordinates = coordinates;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getInspectionProjectsStatus() {
		return inspectionProjectsStatus;
	}

	public void setInspectionProjectsStatus(String inspectionProjectsStatus) {
		this.inspectionProjectsStatus = inspectionProjectsStatus;
	}

	/**
	 * 获取巡查项目
	 * 
	 * @return
	 */
	public String getInspectionProject() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Object>("code", 1)
							.add("result", inspectionService
									.getInspectionProject(flag, inspectionCategories,
											inspectionPointCode.trim())));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}
	
	/**
	 * 提交巡查工单
	 * 
	 * @return
	 */
	public String subInspectionReport() {
		try {
			List<String> inspectionProjectsStatusList = JsonUtil.string2Obejct(
					inspectionProjectsStatus, List.class);
			int result = inspectionService.subInspectionTicket(
					((List<Employee>) request.getSession()
							.getAttribute("users")).get(0).getId(),
					inspectionProjectsStatusList, coordinates, compoundsId);
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 获取所有巡查项目类别
	 * 
	 * @return
	 */
	public String getAllInspectionCategories() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Object>("code", 1)
							.add("result", inspectionService
									.getInspectionCategoriesByPoint(id)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * TODO
	 * 
	 * @return
	 */
	public String getInspectionPointNameByCode() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes(
					"code", 1).add("result", inspectionService.getInspectionPointNameByCode(code, flag)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 获取巡查工单数量根据巡查点分组并根据日期查询
	 * 
	 * @return
	 */
	public String getInspectionTicketCountsGroupByPointByDate() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes("code",
							1).add("result", inspectionService.getInspectionTicketCountsGroupByPointByDate(compoundsId, startTime, endTime)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}
	
	public String getInspectionTicketListGroupByPointByDateTime(){
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", inspectionService.getInspectionTicketListGroupByPointByDateTime(compoundsId, startTime, endTime, hour, id)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}
	
	public static void main(String[] args) {
		System.out.println(MD5Util.getMD5Format(MD5Util.getMD5Format("f4de502e58723e6252e8856d4dc8fc3b")));
	}
	
}
